//
//  SAUpdateInventoryView.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/10/27.
//

#import "SAUpdateInventoryView.h"
#import "SAUpdateInventoryCell.h"
#import "SABatchStockAPI.h"
#import "SAProductOverModel.h"

@interface SAUpdateInventoryView ()<UITableViewDataSource, UITableViewDelegate, HTTPAPIDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *leftMarr;
@property (nonatomic, strong) NSMutableArray *rightMarr;
@property (nonatomic, strong) SABatchStockAPI *batchStockAPI;

@end

@implementation SAUpdateInventoryView

+(instancetype)shareCornView {
    SAUpdateInventoryView * shareView = [[SAUpdateInventoryView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    return shareView;
}

-(instancetype)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        self.backgroundColor = kColor(0, 0, 0, 0.6);
        [self initUI];
    }
    return self;
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    if (self.batchStockAPI == api) {
        [Prompt popPromptViewWithMsg:@"修改成功" duration:2];
        [self shareDissTapEvent];
        !self.refreshBlock ?: self.refreshBlock();
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.leftMarr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SAUpdateInventoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAUpdateInventoryCell" forIndexPath:indexPath];
    [cell refreshLeftTitle:self.leftMarr[indexPath.row] RightTitle:self.rightMarr[indexPath.row]];
    __weak typeof(self) weakSelf = self;
    cell.inputBlock = ^(NSString * _Nonnull str) {
        weakSelf.rightMarr[indexPath.row] = str;
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(41);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return kScaleNum(10);
}

-(void)initUI {
    
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kScaleNum(380));
    [deleteBtn addTarget:self action:@selector(shareDissTapEvent) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:deleteBtn];
    
    UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kScaleNum(380), kScreenWidth, kScaleNum(380))];
    backGroundView.backgroundColor = kColor(246, 246, 246, 1);
    backGroundView.userInteractionEnabled = YES;
    [self addSubview:backGroundView];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.frame = CGRectMake(kScreenWidth - kScaleNum(80), kScaleNum(0), kScaleNum(80), kScaleNum(32));
    [confirmBtn setTitle:@"确认" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:kBuleColor forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(confirmationTapEvent) forControlEvents:UIControlEventTouchUpInside];
    [backGroundView addSubview:confirmBtn];
    
    [backGroundView addSubview:self.tableView];
    
}

- (void)showView:(UIViewController *)Sview {
    if(Sview == nil){
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }else{
        [Sview.view addSubview:self];
    }
}

-(void)confirmationTapEvent {
    NSMutableArray *marr = [NSMutableArray array];
    for (SAProductOverModel *model in self.selectMarr) {
        [marr addObject:@(model.productId)];
    }
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    dict[@"ids"] = marr;
    if (![self.rightMarr[0] isEqualToString:@""]) {
        dict[@"stockUp"] = @([self.rightMarr[0] integerValue]);
    }
    if (![self.rightMarr[1] isEqualToString:@""]) {
        dict[@"stockDown"] = @([self.rightMarr[1] integerValue]);
    }
    self.batchStockAPI.coverParameters = dict;
    [self.batchStockAPI sendRequestToServerWithActivityViewAndMask:POST];
}

-(void)shareDissTapEvent
{
    [UIView animateWithDuration:0.5f animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - lazy
-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(32), kScreenWidth, kScaleNum(348)) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kColor(246, 246, 246, 1);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.scrollEnabled = NO;
        [_tableView registerClass:[SAUpdateInventoryCell class] forCellReuseIdentifier:@"SAUpdateInventoryCell"];
    }
    return _tableView;
}

-(NSMutableArray *)leftMarr {
    if(!_leftMarr) {
        _leftMarr = [NSMutableArray arrayWithObjects:@"库存上限", @"库存下限", nil];
    }
    return _leftMarr;
}

-(NSMutableArray *)rightMarr {
    if(!_rightMarr) {
        _rightMarr = [NSMutableArray array];
        _rightMarr = [NSMutableArray arrayWithObjects:@"", @"", nil];
    }
    return _rightMarr;
}

-(SABatchStockAPI *)batchStockAPI {
    if (!_batchStockAPI) {
        _batchStockAPI = [SABatchStockAPI apiWithDelegate:self];
    }
    return _batchStockAPI;
}

@end
